package test

import (
	utils "gitee.com/ichub/goconfig/common/base/baseutils/stringutils"
	"gitee.com/ichub/gofactroy/common/ichubbase"
	buildfactroy2 "gitee.com/ichub/gofactroy/factroy/dbfactroy"
	"strings"
	"time"
)

type TestFactroy struct {
}

func (factroy *TestFactroy) buildService(tableTool *buildfactroy2.DbFactroy, buildEs bool) string {
	var bs interface{}
	if buildEs {
		bs = buildfactroy2.InstFileFactroy.ReadTestTemplateESSrv()
	} else {
		bs = buildfactroy2.InstFileFactroy.ReadTestTemplateSrv()
	}

	vars := make(map[string]interface{})
	vars["FileName"] = utils.Case2Camel(tableTool.Table) + "Srv_test.go"
	vars["Description"] = "测试服务" + utils.Case2Camel(tableTool.Table) + "Srv"
	vars["Author"] = tableTool.Author
	vars["DATETIME"] = time.Now().Format(ichubbase.FormatDateTime)

	vars["ModelName"] = utils.Case2Camel(tableTool.Table)
	vars["ESModelName"] = utils.Case2Camel(tableTool.Table)
	if buildEs {
		vars["ESModelName"] = "ES" + vars["ESModelName"].(string)
	}
	vars["pkey"] = utils.Case2Camel(tableTool.Pkey)
	vars["pkeyType"] = tableTool.FindGoType(tableTool.PkeyType)
	vars["pkeyField"] = tableTool.Pkey

	vars["StringFieldName"] = utils.Case2Camel(tableTool.StringFieldName)
	vars["StringFieldLen"] = tableTool.StringFieldLen

	vars["PkeyValue"] = "0"
	vars["fmtType"] = "%d"
	if tableTool.FindGoType(tableTool.PkeyType) == "string" {
		vars["PkeyValue"] = "\"\""
		vars["fmtType"] = "%s"
	} else if tableTool.FindGoType(tableTool.PkeyType) == "model.BitField" {
		vars["PkeyValue"] = "false"
		vars["fmtType"] = "%s"
	}
	s := string(bs.([]byte))
	if !buildEs {
		s = strings.ReplaceAll(s, "esservice", "service")
	}
	return utils.ParseTemplateString(s, vars)

}

func (factroy *TestFactroy) buildController(tableTool *buildfactroy2.DbFactroy) string {
	bs := buildfactroy2.InstFileFactroy.ReadTestTemplateController()

	vars := make(map[string]interface{})

	vars["FileName"] = utils.Case2Camel(tableTool.Table) + "Controller_test.go"
	vars["Description"] = "测试API" + utils.Case2Camel(tableTool.Table) + "Controller"
	vars["Author"] = tableTool.Author
	vars["DATETIME"] = time.Now().Format(ichubbase.FormatDateTime)

	vars["ModelName"] = utils.Case2Camel(tableTool.Table)
	vars["lsTableName"] = strings.ToLower(utils.Case2Camel(tableTool.Table))
	vars["ESModelName"] = utils.Case2Camel(tableTool.Table)

	vars["Pkey"] = utils.Case2Camel(tableTool.Pkey)
	vars["pkeyType"] = tableTool.FindGoType(tableTool.PkeyType)
	vars["pkeyField"] = tableTool.Pkey

	vars["StringFieldName"] = utils.Case2Camel(tableTool.StringFieldName)
	vars["StringFieldLen"] = tableTool.StringFieldLen

	vars["PkeyValue"] = "0"
	vars["fmtType"] = "%d"
	if tableTool.FindGoType(tableTool.PkeyType) == "string" {
		vars["PkeyValue"] = "\"\""
		vars["fmtType"] = "%s"
	} else if tableTool.FindGoType(tableTool.PkeyType) == "model.BitField" {
		vars["PkeyValue"] = "false"
		vars["fmtType"] = "%s"
	}
	s := string([]byte(bs))

	return utils.ParseTemplateString(s, vars)

}
func (factroy *TestFactroy) BuildTestServiceFile(dbt *buildfactroy2.DbFactroy) (dir, f, c string) {

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	dir = buildfactroy2.InstFileFactroy.TransDir(buildfactroy2.Gendir_service_test, fbase)

	f, c = fbase+"Srv_test.go", factroy.buildService(dbt, false)
	buildfactroy2.InstFileFactroy.WriteFileModule(dir, f, c)

	return
}

func (factroy *TestFactroy) BuildTestESServiceFile(dbt *buildfactroy2.DbFactroy) (f string, c string) {
	var contents []string

	contents = append(contents, factroy.buildService(dbt, true))

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	tb := fbase + "ESSrv"
	f, c = tb+"_test.go", strings.Join(contents, "\n")

	sdir := buildfactroy2.InstFileFactroy.TransDir(buildfactroy2.Gendir_esservice_test, fbase)

	buildfactroy2.InstFileFactroy.WriteFileModule(sdir, f, c)

	return f, c
}

func (factroy *TestFactroy) buildDAOService(tableTool *buildfactroy2.DbFactroy) string {
	var bs interface{}

	bs = buildfactroy2.InstFileFactroy.ReadTestTemplateDAO()

	vars := make(map[string]interface{})

	vars["FileName"] = utils.Case2Camel(tableTool.Table) + "Dao_test.go"
	vars["Description"] = "测试服务" + utils.Case2Camel(tableTool.Table) + "Dao"
	vars["Author"] = tableTool.Author
	vars["DATETIME"] = time.Now().Format(ichubbase.FormatDateTime)

	vars["ModelName"] = utils.Case2Camel(tableTool.Table)
	vars["ESModelName"] = utils.Case2Camel(tableTool.Table)

	//	vars["ESModelName"] = "ES" + vars["ESModelName"].(string)
	vars["pkey"] = utils.Case2Camel(tableTool.Pkey)
	vars["pkeyType"] = tableTool.FindGoType(tableTool.PkeyType)

	vars["StringFieldName"] = utils.Case2Camel(tableTool.StringFieldName)
	vars["StringFieldLen"] = tableTool.StringFieldLen

	vars["PkeyValue"] = "0"
	vars["fmtType"] = "%d"
	if tableTool.FindGoType(tableTool.PkeyType) == "string" {
		vars["PkeyValue"] = "\"\""
		vars["fmtType"] = "%s"
	}
	s := string(bs.([]byte))

	s = strings.ReplaceAll(s, "esservice", "service")

	return utils.ParseTemplateString(s, vars)

}

func (factroy *TestFactroy) BuildTestDAOFile(dbt *buildfactroy2.DbFactroy) (f string, c string) {
	var contents []string

	contents = append(contents, factroy.buildDAOService(dbt))

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	tb := fbase + "DAO"
	f, c = tb+"_test.go", strings.Join(contents, "\n")

	sdir := buildfactroy2.InstFileFactroy.TransDir(buildfactroy2.Gendir_dao_test, fbase)

	buildfactroy2.InstFileFactroy.WriteFileModule(sdir, f, c)

	return f, c
}

func (factroy *TestFactroy) BuildTestControllerFile(dbt *buildfactroy2.DbFactroy) (dir, f, c string) {

	fbase := utils.Lcfirst(utils.Case2Camel(dbt.Table))
	dir = buildfactroy2.InstFileFactroy.TransDir(buildfactroy2.Gendir_controller_test, fbase)

	f, c = fbase+"Controller_test.go", factroy.buildController(dbt)
	buildfactroy2.InstFileFactroy.WriteFileModule(dir, f, c)

	return
}
func (factroy *TestFactroy) Build(dbt *buildfactroy2.DbFactroy) (dir, f, c string) {
	factroy.BuildTestDAOFile(dbt)

	factroy.BuildTestControllerFile(dbt)

	factroy.BuildTestESServiceFile(dbt)

	dir, f, c = factroy.BuildTestServiceFile(dbt)
	dbt.TestFiles = append(dbt.TestFiles, f)

	return
}
